package com.ahcloud.edas.gatewaylog.biz.application.manager;

import com.ahcloud.edas.common.domain.common.PageResult;
import com.ahcloud.edas.common.exception.BizException;
import com.ahcloud.edas.gatewaylog.biz.application.helper.GatewayLogHelper;
import com.ahcloud.edas.gatewaylog.biz.application.helper.LogParamChecker;
import com.ahcloud.edas.gatewaylog.biz.application.service.GatewayLogService;
import com.ahcloud.edas.gatewaylog.biz.domain.log.query.LogListQuery;
import com.ahcloud.edas.gatewaylog.biz.domain.log.query.LogQuery;
import com.ahcloud.edas.gatewaylog.biz.domain.log.vo.GatewayAccessLogDetailVO;
import com.ahcloud.edas.gatewaylog.biz.domain.log.vo.GatewayAccessLogVO;
import com.ahcloud.edas.gatewaylog.biz.infrastructure.repository.bean.GatewayLog;
import com.ahcloud.edas.gatewaylog.biz.infrastructure.util.PageUtils;
import com.ahcloud.edas.gatewaylog.client.common.DeletedEnum;
import com.ahcloud.edas.gatewaylog.client.exception.LogRetCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @program: ahcloud-operatelog
 * @description:
 * @author: YuKai Fan
 * @create: 2024/6/6 11:04
 **/
@Slf4j
@Component
public class GatewayAccessLogManager {
    @Resource
    private GatewayLogService gatewayLogService;

    /**
     * 分页查询网关日志
     * @param query
     * @return
     */
    public PageResult<GatewayAccessLogVO> pageGatewayLogList(LogListQuery query) {
        LogParamChecker.checkLogListQuery(query);
        logTimeCondition(query);
        PageInfo<GatewayLog> pageInfo = PageHelper.startPage(query.getPageNo(), query.getPageSize())
                .doSelectPageInfo(
                        () -> gatewayLogService.list(
                                new QueryWrapper<GatewayLog>().lambda()
                                        .orderByDesc(GatewayLog::getLogTime)
                                        .eq(GatewayLog::getApiCode, query.getApiCode())
                                        .eq(GatewayLog::getEnv, query.getEnv())
                                        .ge(GatewayLog::getLogTime, query.getStartTime())
                                        .le(GatewayLog::getLogTime, query.getEndTime())
                                        .eq(StringUtils.isNotBlank(query.getTraceId()), GatewayLog::getTraceId, query.getTraceId())
                                        .eq(GatewayLog::getDeleted, DeletedEnum.NO.value)
                        )
                );
        return PageUtils.pageInfoConvertToPageResult(pageInfo, GatewayLogHelper.convertToVOList(pageInfo.getList()));
    }

    /**
     * 查询日志详情
     * @param query
     * @return
     */
    public GatewayAccessLogDetailVO findGatewayAccessLogDetail(LogQuery query) {
        LogParamChecker.checkLogQuery(query, true);
        GatewayLog gatewayLog = gatewayLogService.getOne(
                new QueryWrapper<GatewayLog>().lambda()
                        .eq(GatewayLog::getApiCode, query.getApiCode())
                        .eq(GatewayLog::getEnv, query.getEnv())
                        .eq(GatewayLog::getId, query.getId())
                        .eq(GatewayLog::getDeleted, DeletedEnum.NO.value)
        );
        if (Objects.isNull(gatewayLog)) {
            throw new BizException(LogRetCodeEnum.DATA_NOT_EXISTED);
        }
        return GatewayLogHelper.convertToDetailVO(gatewayLog);
    }

    private void logTimeCondition(LogListQuery query) {
        if (Objects.isNull(query.getStartTime()) && Objects.isNull(query.getEndTime())) {
            LocalDateTime now = LocalDateTime.now();
            // 获取3天前日期
            LocalDateTime startTime = now.minusDays(3);
            query.setStartTime(startTime);
            query.setEndTime(now);
        }
    }
}
